package com.twetec.cms.adapter.pc.backend.controller.statistic;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.twetec.cms.adapter.pc.backend.response.R;
import com.twetec.cms.model.po.statistic.StatisticArticleAddPO;
import com.twetec.cms.model.po.website.SitePO;
import com.twetec.cms.model.vo.statistic.BarChartListVO;
import com.twetec.cms.model.vo.statistic.LineChartListVO;
import com.twetec.cms.model.vo.statistic.LineChartVO;
import com.twetec.cms.service.statistic.StatisticArticleAddService;
import com.twetec.cms.service.statistic.StatisticArticlePageViewsService;
import com.twetec.cms.service.website.SiteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author: zhaichangzheng
 * @Date: 2022/3/5 10:56 （日期和时间）
 */
@Api(tags = "统计文章")
@RestController
@RequestMapping("/statistic/article")
public class StatisticArticlePageViewCountController {

    @Autowired
    private StatisticArticlePageViewsService articlePageViewsService;

    @Autowired
    private StatisticArticleAddService statisticArticleAddService;

    @Autowired
    private SiteService siteService;


    @ApiOperation(value = "每日文章新增统计", notes = "每日文章新增统计")
    @GetMapping("/article-add-count")
    public R<LineChartListVO> getExtendFieldListByColumnId(@ApiParam(value = "站点id", required = true, example = "1234567891") @RequestParam(required = false) Long siteId,
                                                          @ApiParam(value = "开始时间（10位时间戳）", required = true, example = "1234567891") @RequestParam(required = false) Date gmtStartTime,
                                                          @ApiParam(value = "结束时间（10位时间戳）", required = true, example = "1231231231") @RequestParam(required = false) Date gmtEndTime) {
        // 参数校验
        if (null == siteId || 0 == siteId) {
            return R.failure().msg("站点id不能为空");
        }
        if (null == gmtStartTime) {
            return R.failure().msg("开始时间不能为空");
        }
        if (null == gmtEndTime) {
            return R.failure().msg("结束时间不能为空");
        }

        // 获取一段时间内的数据
        QueryWrapper<StatisticArticleAddPO> statisticArticleAddPOQueryWrapper = new QueryWrapper<>();
        statisticArticleAddPOQueryWrapper.eq("SITE_ID", siteId);
        statisticArticleAddPOQueryWrapper.between("GMT_DATE", gmtStartTime, gmtEndTime);
        List<StatisticArticleAddPO> statisticArticleAddPOList = statisticArticleAddService.list(statisticArticleAddPOQueryWrapper);

        // 查询站点名称
        SitePO sitePO = siteService.getById(siteId);
        if (null == sitePO) {
            return R.failure().msg("站点不存在");
        }

        ArrayList<String> xList = new ArrayList<>();
        ArrayList<Long> countList = new ArrayList<>();
        ArrayList<LineChartVO> lineChartVOList = new ArrayList<>();

        // 整理数据
        LineChartVO lineChartVO = new LineChartVO();
        while (gmtStartTime.getTime() <= gmtEndTime.getTime()) {
            // 添加x轴的数据
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String formatDate = sdf.format(gmtStartTime);
            xList.add(formatDate);

            // 添加折线图的对象信息
            Long articleAddCount = 0L;
            for (StatisticArticleAddPO statisticArticleAddPO : statisticArticleAddPOList) {
                if (gmtStartTime.getTime() == statisticArticleAddPO.getGmtDate().getTime()) {
                    articleAddCount = statisticArticleAddPO.getArticleAddCount();
                    break;
                }
            }
            countList.add(articleAddCount);
            gmtStartTime = DateUtil.offsetDay(gmtStartTime,1);
        }
        lineChartVO.setName(sitePO.getName());
        lineChartVO.setCountList(countList);
        lineChartVOList.add(lineChartVO);

        // 准备返回数据
        LineChartListVO lineChartListVO = new LineChartListVO();
        lineChartListVO.setxList(xList);
        lineChartListVO.setDataList(lineChartVOList);

        return R.success().data(lineChartListVO);
    }


}